package gui.other;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Date: Feb 11, 2010
 * Time: 10:40:15 PM
 * @author Jose Martin Rozanec;
 * @authos Maria Julia Racca;
 */
public class JComponentCellRendererStringArrayContext implements TableCellRenderer {
    private Color backgroundColorTrue;
    private Color backgroundColorFalse;
    private Color backgroundColorNotComplete;
    private String[] value;
    private int borderedColumn;

    /**
     * Constructor;
     *
     * @param value - valores de String de la columna para comparar el valor de la celda con el valor
     *              correspondiente en el arreglo;
     */
    public JComponentCellRendererStringArrayContext(String[] value, int borderedColumn) {
        backgroundColorTrue = Color.GREEN;
        backgroundColorFalse = Color.RED;
        backgroundColorNotComplete = Color.YELLOW;
        this.value = value;
        this.borderedColumn=borderedColumn;
    }

    /**
     * Devuelve el componente de la celda;
     *
     * @param table;
     * @param value;
     * @param isSelected;
     * @param hasFocus;
     * @param row;
     * @param column;
     * @return JComponent - componente de la celda;
     */
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (value != null) {
            if(value.getClass().equals((new String()).getClass())){
                value = new JLabel((String)value);
            }
            ((JComponent) value).setOpaque(true);
            JLabel label = (JLabel) value;
            if (label.getText().equals(this.value[row])) {
                ((JComponent) value).setBackground(backgroundColorTrue);
            } else {
                if (!label.getText().equalsIgnoreCase("v") && !label.getText().equalsIgnoreCase("f")) {
                    ((JComponent) value).setBackground(backgroundColorNotComplete);
                    label.setText(this.value[row]);
                } else {
                    ((JComponent) value).setBackground(backgroundColorFalse);
                }
            }
        }else{
            value = new JLabel(this.value[row]);
            ((JComponent) value).setBackground(backgroundColorNotComplete);
        }
        if(column==borderedColumn){
            if(row==0){
                ((JComponent) value).setBorder(BorderFactory.createMatteBorder(3,3,0,3,Color.black));
            }else{
                if(row==(this.value.length-1)){
                    ((JComponent) value).setBorder(BorderFactory.createMatteBorder(0,3,3,3,Color.black));
                }else{
                    ((JComponent) value).setBorder(BorderFactory.createMatteBorder(0,3,0,3,Color.black));
                }
            }
        }

        ((JComponent) value).revalidate();
        return (JComponent) value;
//
//        if (value != null) {
//            ((JComponent) value).setOpaque(true);
//            if (((JLabel) value).getText().equals(this.value)) {
//                ((JComponent) value).setBackground(backgroundColorTrue);
//            } else {
//                ((JComponent) value).setBackground(backgroundColorFalse);
//
//            }
//            ((JComponent) value).revalidate();
//        }
//        return (JComponent) value;
    }
}